import { ref, watch,computed} from "vue";
import { defineStore } from "pinia";

import { fetchData } from "@/api/event";

export const useEventStore = defineStore(
  "event",
  () => {
    const eventList = ref([]);
    const newEvent = ref([
      {
        id: "SJ202501027788",
        type: "刮擦",
        level: "2",
        location: "光谷大道o大道o",
        carnumber: "鄂A12345",
        name: "张三",
        status: "0",
        time: "2025.01.02 00:00",
        position: [114.36947822570801, 30.484399795532227],
      },
      {
        id: "SJ202501027789",
        type: "失火",
        level: "3",
        location: "光谷大道o大道o",
        carnumber: "鄂A12345",
        name:'李四',
        status: "0",
        time: "2025.01.02 00:00",
        position: [114.37, 30.50],
      }
    ]);
    const newEventCount = computed(() => newEvent.value.length);

    // 初始化时获取数据
    fetchData().then((res) => {
      res.forEach((item) => {
        if (item.getProperties && item.getProperties().values_) {
          eventList.value.push(item.getProperties().values_);
        }
      });
    });

    // 定义新增事件函数
    const addNewEvent = (event) => {
      if (event) {
        newEvent.value.push(event);
        console.log(newEvent.value);
      }
    };

    // 定义删除事件函数
    const deleteNewEvent = (index)=>{
        newEvent.value.splice(index,1);
    }


    return { eventList, newEvent, addNewEvent, newEventCount,deleteNewEvent };
  },
  {
    persist: true,
  }
);
